package com.getguang.superstore.home

import android.os.Build
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.getguang.superstore.databinding.ActivitySsdHomeBinding
import com.getguang.superstore.home.list.DealsListFragment
import com.starblink.android.basic.base.activity.BaseTVMActivity
import com.starblink.android.basic.ext.addShowFragment
import com.starblink.android.basic.ext.dp
import com.starblink.android.basic.extension.extraOf
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.basic.route.RoutePage

/**
 * Super Store Deal 首页
 *  Create by yuanl at 2023/10/8
 **/
@Route(path = RoutePage.SuperStoreDeal.HOME)
class SSDealHomeActivity : BaseTVMActivity<ActivitySsdHomeBinding, SSDealHomeVM>() {

    override fun isStatusBarLightMode(): Boolean = true

    override fun initViewBinding(): ActivitySsdHomeBinding =
        ActivitySsdHomeBinding.inflate(layoutInflater)

    @JvmField
    @Autowired(name = "deals")
    var deals: Array<String>? = null

    override fun initData() {
        super.initData()
    }


    override fun initView() {
        super.initView()
        viewBinding.appbar.addOnOffsetChangedListener { appBarLayout, verticalOffset ->
            val progress =
                1f - kotlin.math.abs(verticalOffset.toFloat()) / appBarLayout.totalScrollRange

            viewBinding.tvTitle1.alpha = progress * progress
            viewBinding.tvTitle2.alpha = progress * progress

            viewBinding.vSearchBar.setPadding(
                12.dp + ((viewBinding.vBack.right) * (1f - progress)).toInt(),
                (6.dp * (1f - progress)).toInt(),
                12.dp,
                (6.dp + 6.dp * (progress)).toInt()
            )

        }
        viewBinding.vSearchBar.trackData(
            spmPageId(),
            SpmElementDef.HomeNewSearchBar_33002,
            onlyHit = true,
        )
        viewBinding.vSearchBar.setOnClickListener {
            navigationTo(RoutePage.SuperStoreDeal.SEARCH) {
                extraOf(
                    "deals" to deals
                )
            }
        }
        addShowFragment(DealsListFragment.newIns(deals), viewBinding.container.id)

    }

    override fun spmPageId(): Int = SpmPageDef.PAGE_10239

    override fun isSpmTrackWithLifecycle(): Boolean = true

}